package com.gsi.webstore.platform.storefront.pipelet;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineInitializationException;

/**
 * Returns lat long data for a static set of zip codes
 */
public class MockGeopointLookup extends Pipelet
{
    
    Map<String,String[]> lookupTable = new HashMap<String,String[]>();

    /**
     * Constant used to access the pipeline dictionary with key 'ZipCode'
     */
    public static final String DN_ZIP_CODE = "ZipCode";
    /**
     * Constant used to access the pipeline dictionary with key 'Latitude'
     */
    public static final String DN_LATITUDE = "Latitude";
    /**
     * Constant used to access the pipeline dictionary with key 'Longitude'
     */
    public static final String DN_LONGITUDE = "Longitude";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    throws PipeletExecutionException {        
        // lookup 'ZipCode' in pipeline dictionary
        String zipCode = (String)dict.get(DN_ZIP_CODE);
        if (null == zipCode)
        {
            throw new PipeletExecutionException("Mandatory input parameter 'ZipCode' not available in pipeline dictionary.");
        }


        String[] result = lookupTable.get(zipCode);
        if (result!=null) {
        
            // store 'Latitude' in pipeline dictionary
            dict.put(DN_LATITUDE, result[0]);
            
            // store 'Longitude' in pipeline dictionary
            dict.put(DN_LONGITUDE, result[1]);
        
        }
        return PIPELET_NEXT;
    }

    @Override
    public void init() throws PipelineInitializationException
    {
        super.init();
        List<String[]> dataList = new ArrayList<String[]>();
        populateList(dataList);
        for (String[] item:dataList) {
            lookupTable.put(item[0],new String[] {item[1],item[2]});
        }
        
        
    }

    private void populateList(List<String[]> dataList) {
        InputStream is = getClass().getClassLoader().getResourceAsStream("ISL_POSTAL_CODES.csv");
        LineNumberReader lnr = new LineNumberReader(new BufferedReader(new InputStreamReader(is)));
        String line = null;
        do {
            try
            {
                line=lnr.readLine();
            }
            catch(IOException e)
            {
                line=null;
                break;
            }
            if (line!=null) dataList.add(line.split(","));
        } while (line != null);
        try
        {
            lnr.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        
    }
    
    
    
}